﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.FTPClient;
using System.Data.OracleClient;
using System.IO;
using WPFExcelReport.Helpers;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    public class ITXPRNReport : BaseReport
    { 
        System.IO.StreamWriter writer; 
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public ITXPRNReport()
            : base()
        {
             
           
            Root = "C:\\IntexReport";
             
        }
        
        public override void WriteExcelReport(string fileType)
        { 
            try
            {
                //exAgent._app.Visible = true;
                Helper.GetDate(ref date, ref year, ref month, ref day);
                strCompany = "JLT";
                destfolder = SetFolder(Root, "标签");
                DataRow dwData = dsSource.Tables["ICC_PRNDEF"].Rows[0];
                string newfilename = dwData["PRNAME"].ToString().Trim();
                ServerShareFile(newfilename, fileType); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ServerShareFile(string ordernumer, string fileType)
        {
            string destfile, tempfile = "";
            string fileabsname = ordernumer;
            try
            {
                filename = destfolder + "\\" + fileabsname + ".PRN"; ;
                destfile = filename;   
                if (File.Exists(destfile))
                {
                    File.Delete(destfile);
                }
                CreateFile(destfile);

                //File.Move(tempfile, destfile); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
        }
        private void FillInEXCEL()
        {
            try
            {
                var query = from DataRow row in dsSource.Tables["ICC_PRNDEF"].AsEnumerable()
                            orderby Convert.ToDecimal(row.Field<string>("PRLY")),
                           Convert.ToDecimal(row.Field<string>("PRLX"))
                            select row;
                foreach (DataRow row in query)
                {
                    //PRLX	PRLY	PRRX	PRRY	PRLTHICK	PRCTYPE	 	PRFONT	PRFONTSIZE	PRCONTENT
                    string text = "";
                    string PRLX = row["PRLX"].ToString().Trim();
                    string PRLY = row["PRLY"].ToString().Trim();
                    string PRRX = row["PRRX"].ToString().Trim();
                    string PRRY = row["PRRY"].ToString().Trim();
                    string PRLTHICK = row["PRLTHICK"].ToString().Trim();
                    string PRCTYPE = row["PRCTYPE"].ToString().Trim();
                    string PRFONT =GetGB2312Value(row["PRFONT"].ToString().Trim());
                    string PRFONTSIZE = row["PRFONTSIZE"].ToString().Trim();

                    string PRCONTENT = GetGB2312Value(row["PRCONTENT"].ToString().Trim());
                    string root = row["PRCONTDES"].ToString().Trim();
                    switch (PRCTYPE)
                    { 
                        case "X":
                            //X,40,40,4,1180,560
                            //PRLX	PRLY	PRRX	PRRY	PRLTHICK
                            text = "X," + PRLX + "," + PRLY + "," + PRLTHICK + "," + PRRX + "," + PRRY;
                            break;
                        case "H":
                            //H,40,152,1140,2
                            text = "H," + PRLX + "," + PRLY + "," + PRRX + "," + PRLTHICK;
                            break;
                        case "G":
                            //B,50,390,0,3,4,8,100,N,\48
                            text = "G," + PRLX + "," + PRLY + ","+root+"\\"+ PRCONTENT;
                            break;
                        case "A":
                            //A,微软雅黑,50,50,320,0,4,1,2,N,客户品种Yours:\N4
                            text = "A," + PRFONT + "," + PRFONTSIZE + "," + PRLX + "," + PRLY + ",0,4,1,2,N," + PRCONTENT.Replace("_T","\\");
                            break;
                        case "B":                      
                            //B,50,390,0,3,4,8,100,N,\48
                            text = "B," +  PRLX + "," + PRLY + ",0,3,4,8,"+PRRY+",N," + PRCONTENT.Replace("_T", "\\");
                            break;
                        case "b":
                            //b30,200,Q,m1,s3,eM,iA,"0053 33 -59"
                            text = "b," + PRLX + "," + PRLY + ",Q,m1,s3,eM,iA," + PRCONTENT.Replace("_T", "\\");
                            break;
                    }
                    //writer.Flush();
                    writer.WriteLine(text);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private string CreateFile(string filename)
        {

            try
            {
                writer = new System.IO.StreamWriter(filename,true,System.Text.Encoding.GetEncoding("GB2312"));
                writer.WriteLine("nop_front");
                writer.WriteLine("GK"+'"'+"*"+'"');
                writer.WriteLine("nop_middle");
                writer.WriteLine("N");
                writer.WriteLine("R0,0");
                writer.WriteLine("OD");
                writer.WriteLine("D8");  
                FillInEXCEL(); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                writer.Close();
            }

            return filename;
        }
    }

}
